/*
 * Amaze - QT downloader for the Amazon MP3 store
 * Copyright (c) 2010-2011 Robert Keevil
 *
 * This program is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef AMAZE_H
#define AMAZE_H

#include <QMainWindow>
#include <QTableWidget>
#include <QPushButton>
#include <QLabel>
#include <QFile>
#include <QAction>
#include <QGridLayout>
#include <QMenuBar>
#include <QStatusBar>
#include <QMutex>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QMap>
#include "common.h"
#include "about.h"
#include "config.h"
#include "format_help.h"
#include "license.h"

class MainWindow;
class Parse;
class Settings;
class Download;
class Console;
class QAction;

class Amaze : public QMainWindow {
    Q_OBJECT
public:
    friend class Settings;
    friend class Download;
    friend class Parse;
    friend class Console;
    Amaze(QWidget *parent = 0);
    ~Amaze();
    void clear_log();
    void set_log_level(LOG_LEVEL);

protected:
    Config *config;
    QList<log_entry> *log_messages;

private:
    void setup_window();
    void setup_widgets();
    void setup_connects();
    void open(QString filepath);
    void make_paths();
    void update_table();
    void update_status();
    void create_folders();
    void update_aa(int index);

    QFile *target;
    QList<track> *tracks;
    LOG_LEVEL log_print_level;
    QString error_str;
    QMutex *mutex;
    QStringList table_labels;

    About *about_dialog;
    Settings *settings_dialog;
    Console *console_dialog;
    Download *dl;
    Parse *parser;
    Format_Help *format_dialog;
    License *license_dialog;

    QNetworkAccessManager *aa_manager;
    QNetworkRequest aa_request;
    QNetworkReply *aa_reply;
    QMap<QString, QPixmap *> aa_map;
    QString aa_url;
    QString aa_url_requested;

    QAction *act_open;
    QAction *act_exit;
    QAction *act_about;
    QAction *act_about_qt;
    QAction *act_format_help;
    QAction *act_license;
    QAction *act_settings;
    QAction *act_console;
    QWidget *central_widget;
    QTableWidget *track_table;
    QPushButton *btn_open;
    QPushButton *btn_download;
    QMenuBar *menu_bar;
    QMenu *menu_file;
    QMenu *menu_help;
    QStatusBar *statusbar;
    QLabel *lbl_status;
    QFrame *btm_frame;
    QLabel *btm_aa;
    QLabel *lbl_albumartist;
    QLabel *lbl_artist;
    QLabel *lbl_album;
    QLabel *lbl_title;
    QLabel *lbl_genre;
    QLabel *lbl_length;
    QLabel *lbl_filesize;
    QLabel *info_albumartist;
    QLabel *info_artist;
    QLabel *info_album;
    QLabel *info_title;
    QLabel *info_genre;
    QLabel *info_length;
    QLabel *info_filesize;

    // keep synced with the track_labels in Amaze::setup_widgets()
    enum {
        COL_ARTIST,
        COL_ALBUM,
        COL_TITLE,
        COL_TRACKNUM,
        COL_STATUS,
        COL_PROGRESS,
        COL_NUM_COL
    };

private slots:
    void about();
    void about_qt();
    void console();
    void dl_started(int track_index);
    void dl_progress(int track_index, qint64 bytes_current, qint64 bytes_total);
    void download();
    void download_finished(int track_index, bool success, QString error_msg);
    void format();
    void license();
    void open();
    void settings();
    void settings_close();
    void parsed();
    void add_log(LOG_LEVEL, QString);
    void update_panel();
    void aa_get_finished();
};

#endif // AMAZE_H
